/**
 * Project Name:SinaSports
 * File Name:SubViewPager.java
 * Package Name:cn.com.sina.sports.widget
 * Date:2014-7-28下午6:41:14
 * Copyright (c) 2014, hushuan@staff.sina.com.cn All Rights Reserved.
 *
 */

package artid.com.arttogo.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * ClassName:SubViewPager <br/>
 * Date: 2014-7-28 下午6:41:14 <br/>
 * 
 * @author hushuan
 * @version
 * @since JDK 1.6
 * @see
 */
public class SubViewPager extends ViewPager {
	/**
	 * Position of the last motion event.
	 */
	private float mLastMotionX, mLastMotionY;
	/**
	 * ID of the active pointer. This is used to retain consistency during
	 * drags/flings if multiple pointers are used.
	 */
	protected int mActivePointerId = INVALID_POINTER;
	/**
	 * Sentinel value for no current active pointer. Used by
	 * {@link #mActivePointerId}.
	 */
	private static final int INVALID_POINTER = -1;

	public SubViewPager(Context context) {
		super(context);
	}

	public SubViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mLastMotionX = ev.getX();
			mLastMotionY = ev.getY();
			getParent().requestDisallowInterceptTouchEvent(true);
			break;
		case MotionEvent.ACTION_MOVE:
			final float x = ev.getX();
			final float y = ev.getY();
			final float xDistance = Math.abs(mLastMotionX - x);
			final float yDistance = Math.abs(mLastMotionY - y);
			if (xDistance > yDistance) {
				return true;
			} else {
				return false;
			}
		}
		return super.onInterceptTouchEvent(ev);
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mLastMotionX = ev.getX();
			mLastMotionY = ev.getY();
			getParent().requestDisallowInterceptTouchEvent(true);
			break;
		case MotionEvent.ACTION_MOVE:
			final float x = ev.getX();
			final float dx = x - mLastMotionX;
			if (isFistItem() && dx > 0) {
				getParent().requestDisallowInterceptTouchEvent(false);
			} else if (isLastItem() && dx < 0) {
				getParent().requestDisallowInterceptTouchEvent(false);
			} else {
				getParent().requestDisallowInterceptTouchEvent(true);
			}
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			break;
		}
		return super.onTouchEvent(ev);
	}

	private boolean isFistItem() {
		return 0 == getCurrentItem();
	}

	private boolean isLastItem() {
		if (null == getAdapter()) {
			return true;
		}
		return getCurrentItem() >= (getAdapter().getCount() - 1);
	}

}
